<?php
require_once 'config21.php';
// 验证登录状态
if (!isset($_SESSION['userid'])) {
    header("Location: login21.php");
    exit;
}

// 获取记录id并验证
$no = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($no <= 0) {
    die('无效的记录ID');
}

// 错误消息数组
$errors = [];
$success = false;

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit_introduce'])) {
    // 获取并过滤输入
    $name = trim($_POST['name']);
    $anothername = trim($_POST['anothername']);
    $intro = trim($_POST['intro']);
    $datetime = date('Y-m-d H:i:s'); // 自动更新时间

    // 验证必填字段
    if (empty($name)) $errors[] = '名称不能为空';
    if (empty($intro)) $errors[] = '介绍内容不能为空';

    // 如果没有错误，执行数据库更新
    if (empty($errors)) {
        try {
            // 准备SQL语句
            $sql = "UPDATE introduce 
                   SET name=?, anothername=?, intro=?, datetime=? 
                   WHERE no=?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param('ssssi', $name, $anothername, $intro, $datetime, $no);

            // 执行更新
            if ($stmt->execute()) {
                $success = true;
                $_SESSION['success_message'] = "鸟类介绍修改成功";
                header("Location: list_introduce21.php");
                exit;
            } else {
                $errors[] = '数据库更新失败:' . $stmt->error;
            }

            $stmt->close();
        } catch (Exception $e) {
            $errors[] = '发生错误:' . $e->getMessage();
        }
    }
} else {
    // 获取现有记录数据
    $sql = "SELECT * FROM introduce WHERE no=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $no);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows == 0) {
        die('未找到该记录!');
    }

    $info = $result->fetch_assoc();
    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改鸟类介绍</title>
    <link rel="stylesheet" href="style21.css">
    <style>
        .error-message {
            color: red;
            margin-bottom: 10px;
        }
        textarea {
            width: 100%;
            min-height: 200px;
            resize: vertical;
        }
    </style>
</head>
<body>
    <header>
        <h1>修改鸟类介绍</h1>
    </header>
    <nav>
        <a href="index.php">首页</a>
        <a href="list_nationalbird21.php">国家鸟类列表</a>
        <a href="list_introduce21.php">鸟类介绍列表</a>
        <a href="logout21.php">退出登录</a>
    </nav>
    <div class="container">
        <!-- 显示错误信息 -->
        <?php if (!empty($errors)) : ?>
            <div class="error-message">
                <ul>
                    <?php foreach ($errors as $error) : ?>
                        <li><?php echo $error; ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        
        <form method="post" name="form1" id="form1">
            <table width="100%" border="1">
                <tbody>
                    <tr>
                        <td colspan="2" align="center">修改鸟类介绍</td>
                    </tr>
                    <tr>
                        <td>名称</td>
                        <td><input type="text" name="name" id="name" value="<?php echo htmlspecialchars($info['name']); ?>" required></td>
                    </tr>
                    <tr>
                        <td>别名</td>
                        <td><input type="text" name="anothername" id="anothername" value="<?php echo htmlspecialchars($info['anothername']); ?>"></td>
                    </tr>
                    <tr>
                        <td>介绍内容</td>
                        <td><textarea name="intro" id="intro" required><?php echo htmlspecialchars($info['intro']); ?></textarea></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" name="edit_introduce" id="submit" value="修改">
                            &nbsp;&nbsp;&nbsp;
                            <input type="reset" name="reset" id="reset" value="重置">
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>
</body>
</html>